using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Decav.Reality;
using Decav.Gatsb.Application;
using Decav.Gatsb;
using Decav.Gatsb.Configuration;
using Decav.Gatsb.Web;
using Decav.Gatsb.Data;

public partial class ValidatePhone : System.Web.UI.Page
{
    /// <summary>
    /// Gets or sets where this session redirects to after creating a user account.
    /// </summary>
    public string SessionRedirect
    {
        get
        {
            return Session["CreateAccount_Redirect"] as string;
        }
        set
        {
            Session["CreateAccount_Redirect"] = value;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        GatsbWebApplication.EnsureAuthenticatedSession();

        invalidNewNumberErrorDiv.Visible = false;
        invalidErrorDiv.Visible = false;
        resendDiv.Visible = false;
        SetupLabels();

        ClientScript.RegisterStartupScript(this.GetType(), "SetPrimaryFocus",
            string.Format("document.getElementById(\"{0}\").focus()", validationNumberText.ClientID), true);
    }

    private void SetupLabels()
    {
        telNumberLiteral.Text = GatsbApplication.Current.CurrentUser.MobilePhoneNumber.ToString();
        telNumberLiteral2.Text = GatsbApplication.Current.CurrentUser.MobilePhoneNumber.ToString();
        telProviderLiteral.Text = GatsbApplication.Current.CurrentUser.TelephoneServiceProvider.Name;
    }

    protected void SubmitButton_Click(object sender, EventArgs e)
    {
        Validate();
        if (!IsValid)
            return;

        if (GatsbApplication.Current.TryValidateUserCode(new UserValidationCode(validationNumberText.Text)))
        {
            FormsAuthentication.SetAuthCookie(GatsbApplication.Current.CurrentUser.Email.ToString(), false);

            if (SessionRedirect != null)
            {
                // Redirect to where the account was created.
                SessionRedirect = null;
                Response.Redirect(SessionRedirect, true);
            }
            else
            {
                Response.Redirect("~/GettingStarted.aspx", true);
            }
        }
        else
        {
            invalidErrorDiv.Visible = true;
        }
    }

    protected void ChangeNumber_Click(object sender, EventArgs e)
    {
        if (!PhoneNumber.IsValid(wrongPhoneNumberTextBox.Text))
        {
            invalidNewNumberErrorDiv.InnerText = "Please enter a 10 digit number and choose your provider in order to change your mobile phone number.";
            invalidNewNumberErrorDiv.Visible = true;
            return;
        }

        PhoneNumber telNumber = new PhoneNumber(wrongPhoneNumberTextBox.Text);

        using (IGatsbDataAdapter adapt = GatsbApplication.Current.GetDataAdapter())
        {
            if (adapt.CheckUserExists(telNumber))
            {
                // Error
                invalidNewNumberErrorDiv.Visible = true;
                invalidNewNumberErrorDiv.InnerText = "The number you supplied is already in use.";
                return;
            }

            Decav.Gatsb.User usr = GatsbApplication.Current.CurrentUser;
            usr.MobilePhoneNumber = telNumber;
            usr.TelephoneServiceProvider = TelephoneServiceProvider.GetProvider(phoneProviderDropDown.SelectedType);
    
            adapt.SetupUpdatePhone(usr.Id, telNumber, usr.TelephoneServiceProvider);
        }

        SetupLabels();
        GatsbApplication.Current.SendSmsValidation();
        resendDiv.Visible = true;
        
    }

    protected void ResendLink_Click(object sender, EventArgs e)
    {
        GatsbApplication.Current.SendSmsValidation();
        resendDiv.Visible = true;
    }
}
